package filesearch

import groovy.lang.Closure;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import groovy.util.AbstractFactory
import groovy.util.FactoryBuilderSupport

import java.util.Map

import javax.swing.JTable;

import org.viewaframework.widget.swing.table.DynamicTable
import org.viewaframework.widget.swing.table.DynamicTableColumn
import org.viewaframework.widget.swing.table.DynamicTableModel

/**
 * This class builds instances of {@link DynamicTable}. It has a tricky estructure due to
 * the {@link DynamicTable} constructors.
 * 
 * @author mgg
 *
 */
class ViewaWidgetBuilder extends FactoryBuilderSupport{
	
	def childModel
	
	/**
	 * Default constructor
	 */
	public ViewaWidgetBuilder(){
		super()
	 /* Registering the factories */
		registerFactories()	
	}

	/**
	 * This method registers the different factories
	 * 
	 * @return
	 */
	def registerFactories(){
		registerFactory('dynamicTable', new DynamicTableFactory())
		registerFactory('dynamicColumns', new DynamicTableModelFactory())
		registerFactory('dynamicColumn', new DynamicTableColumnFactory())		
	}

	/* (non-Javadoc)
	 * @see groovy.util.FactoryBuilderSupport#postNodeCompletion(java.lang.Object, java.lang.Object)
	 */
	@Override
	protected Object postNodeCompletion(Object arg0, Object arg1) {
		def node = super.postNodeCompletion(arg0, arg1);
	 /* When the DynamicTableModel has been built then I store it to 
	  * add it later to the DynamicTable */
		if (node instanceof List){
			childModel = new DynamicTableModel(node)
	 /* When all children has been built then I return the root node with
	  * the proper constructor */
		} else if (node instanceof JTable){
			node = new DynamicTable(childModel)
		}
		return node
	}

	/**
	 * The DynamicTable factory is just a dummy factory to allow children to be built
	 * 
	 * @author mgg
	 *
	 */
	private class DynamicTableFactory extends AbstractFactory{
		def newInstance(FactoryBuilderSupport factoryBuilderSupport,Object name,Object value,Map map){
		 /* It has to return something at this point, so it returns a dummy JTable object */
			return new JTable()
		}			
	}
	
	/**
	 * This factory builds instances of {@link DynamicTableModel}
	 * 
	 * @author mgg
	 *
	 */
	private class DynamicTableModelFactory extends AbstractFactory{
		def newInstance(FactoryBuilderSupport factoryBuilderSupport,Object name,Object value,Map map){
			return new ArrayList()
		}
		public void setChild(FactoryBuilderSupport factoryBuilderSupport,Object parent,Object child){			
			if (child instanceof DynamicTableColumn){
			 /* If children are of type DynamicTableColumn then they're added to the model */
				parent << child				
			}
		}
	}
	
	/**
	 * This factory builds instances of {@link DynamicTableColumn}
	 * 
	 * @author mgg
	 *
	 */
	private class DynamicTableColumnFactory extends AbstractFactory{
		def newInstance(FactoryBuilderSupport factoryBuilderSupport,Object name,Object value,Map map){
			return new DynamicTableColumn(propertyName:map.propertyName,order:map.order,width:map.width,renderer:map.renderer)
		}	
	}	
}



